<?php
class XulyController extends AppController {
	public $uses = array('Hoso','Tthc','ThanhPhan','ThanhPho','ThanhPhanTthc','Status','User','LichsuHoso','Attachment','Ketquahanhchinh','LinhVuc',
		'LoaiHinh','PhieuTrinh','ThongKe','ChiTieuThongKe','ChiTieuPhanLoai','PhanLoaiHoso');
	
	public function beforeFilter() {
		$this->layout = 'quanly';
	}
	
	public function index($status = null) {
		$st = array(
			$this->Status->CHO_XULY,
			$this->Status->HS_KHONG_DAT,
			$this->Status->BO_SUNG_HS,
			$this->Status->HS_DU_YEU_CAU ,
			$this->Status->HS_DA_XULY_XONG,
			$this->Status->LAY_HS_TON_DONG,
			$this->Status->HS_CHO_KIEM_DUYET,
			$this->Status->HS_CHO_KY,
			$this->Status->HS_CHO_TRA_KQ,
			$this->Status->HS_DA_LUU_TRU,
			$this->Status->HS_TON_DONG,
			$this->Status->HS_DANG_XULY,
			$this->Status->HS_KHONG_DUOC_DUYET,
			$this->Status->HS_DA_DUYET,
			$this->Status->HS_KHONG_DUOC_KY,
			$this->Status->HS_DA_KY,
			$this->Status->HS_DA_TRA_KQ,
			$this->Status->HS_TRE_HEN,
		);
			
		if (!empty($status) && in_array($status, $st)) {
			$st = array($status);
		}
		$listStatus = array(
			1 => 'All',
			$this->Status->CHO_XULY => 'Chờ xử lý',
			$this->Status->HS_KHONG_DAT => 'Hồ sơ không đạt',
			$this->Status->BO_SUNG_HS => 'Hồ sơ cần bổ xung',
			$this->Status->HS_DU_YEU_CAU => 'Hồ sơ đủ yêu cầu' ,
			$this->Status->HS_DA_XULY_XONG => 'Hồ sơ đã xử lý xong',
			$this->Status->LAY_HS_TON_DONG => 'Hồ sơ tồn đọng',
			$this->Status->HS_CHO_KIEM_DUYET => 'Hồ sơ chờ kiểm duyệt',
			$this->Status->HS_CHO_KY => 'Hồ sơ chờ ký',
			$this->Status->HS_CHO_TRA_KQ => 'Hồ sơ chờ trả kết quả',
			$this->Status->HS_DA_LUU_TRU => 'Hồ sơ lưu trữ',
			$this->Status->HS_TON_DONG => 'Hồ sơ tồn đọng',
			$this->Status->HS_DANG_XULY => 'Hồ sơ đang xử lý',
			$this->Status->HS_KHONG_DUOC_DUYET => 'Hồ sơ không được duyệt',
			$this->Status->HS_DA_DUYET => 'Hồ sơ đã duyệt',
			$this->Status->HS_KHONG_DUOC_KY => 'Hồ sơ không được ký',
			$this->Status->HS_DA_KY => 'Hồ sơ đã ký',
			$this->Status->HS_DA_TRA_KQ => 'Hồ sơ đã trả kết quả',
			$this->Status->HS_TRE_HEN => 'Hồ sơ trễ hẹn');
			
		$this->set('listStatus', $listStatus);
		$this->set('status', $status);
		
		$listHosos = $this->Hoso->find("all", array(
			'conditions' => array('Hoso.trang_thai' => $st, 'Hoso.nguoi_xuly_id' => $this->Auth->user('id')) 
		));
		
		
		$this->set('listHosos', $listHosos);
	}
	
	public function xuly ($id = null) {
		$inStatus = array(
			$this->Status->CHO_XULY,
			$this->Status->HS_DANG_XULY,
			$this->Status->HS_KHONG_DAT,
			$this->Status->BO_SUNG_HS,
			$this->Status->HS_DU_YEU_CAU,
			$this->Status->HS_DA_XULY_XONG,
			$this->Status->LAY_HS_TON_DONG,
			$this->Status->HS_TON_DONG,
			$this->Status->HS_KHONG_DUOC_DUYET,
			$this->Status->HS_KHONG_DUOC_KY,
		);
		$hoso = $this->Hoso->findById($id);
		$date1 = new DateTime();
		$date2 = new DateTime($hoso['Hoso']['ngay_hen_tra']);
		// debug($hoso);
		// update noi dung xu ly
		if (!empty($this->request->data)) {
			debug($this->request->data);
			$hoso = $this->Hoso->findById($this->request->data['Hoso']['id']);
			$this->Hoso->id = $this->request->data['Hoso']['id'];
			$this->Hoso->saveField('noidungxuly', $this->request->data['Hoso']['noidungxuly']);
			
			if (!empty($this->request->data['Hoso']['status']) && in_array($this->request->data['Hoso']['status'], $inStatus)) {
				$this->Hoso->saveField('trang_thai', $this->request->data['Hoso']['status']);
			} else {
				$this->Hoso->invalidate('trang_thai', __('Chọn trang thái kết luận khi lưu', true));
			}
			
			$date2 = new DateTime($hoso['Hoso']['ngay_hen_tra']);
			
			//$this->Hoso->saveField('so_ngay_con_lai', $this->compareDate($date1, $date2));
			$this->Hoso->save();
			
			// update history
			
			$his = array(); $his['LichsuHoso'] = array();
			$his['LichsuHoso']['user_id'] = $this->Auth->user('id');
			$his['LichsuHoso']['hoso_id'] = $this->Hoso->id;
			$his['LichsuHoso']['noi_dung_giai_quyet'] = $this->request->data['Hoso']['noidungxuly'];
			$his['LichsuHoso']['thao_tac'] = 'Xử lý hồ sơ';
			$his['LichsuHoso']['so_ngay_con_lai'] = $this->compareDate($date1, $date2);
			
			if (in_array($this->request->data['Hoso']['status'], $inStatus)) {
				$his['LichsuHoso']['status'] = $this->request->data['Hoso']['status'];
				$this->LichsuHoso->create();
				$this->LichsuHoso->save($his);
			}
			// redirect after saving succeed
			$this->redirect(array('action' => 'xuly', $this->request->data['Hoso']['id']));
		}
		
		$thanhPhos = $this->ThanhPho->find("list", array('fields'=>array('code','ten')));
		$this->set('thanhPhos', $thanhPhos);
		
		$this->User->recursive = -1;
		
		$users = $this->User->find("list", array("conditions" => array("User.username !=" => 'admin'), 'fields'=>array('id','username')));
		$his = $this->LichsuHoso->find("all", array("conditions" => array("LichsuHoso.hoso_id" => $id)));
		
		$this->set("enable", 0);
		
		if(in_array($hoso['Hoso']['trang_thai'], $inStatus)) {
			$this->set("enable", 1);
		}
		
		// calculate ngay qua han
		($this->compareDate($date1, $date2) < 0) && $this->set("qua_han", $this->compareDate($date1, $date2));
		
		// debug($users);
		$this->set("his", $his);
		$this->set("hoso", $hoso);
		$this->set("users", $users);
		$this->set("selectedHoso", $id);
	}
	
	public function chuyenTraketqua ($id = null) {
		if (!empty($this->request->params['pass'][0])) {
			$selections = split(",",$this->request->params['pass'][0]."-1");
			foreach ($selections as $key => $id) {
				$this->Hoso->recursive = -1;
				$hoso = $this->Hoso->findById($id);
				if (!empty($hoso) && ($hoso['Hoso']['trang_thai'] == $this->Status->HS_DA_KY || $hoso['Hoso']['trang_thai'] == $this->Status->HS_KHONG_DAT)) {
					$this->Hoso->id= $id;
					$this->Hoso->saveField('trang_thai', $this->Status->HS_CHO_TRA_KQ);
					$this->Hoso->save();
				}
			}
		} else {
			$this->log("Rejecting document: invalid document id= "+$id);
		}
		// $this->render('/index.ctp');
		$this->redirect('index');
	}
	
	public function luutru ($id = null) {
		if (!empty($this->request->params['pass'][0])) {
			$selections = split(",",$this->request->params['pass'][0]."-1");
			foreach ($selections as $key => $id) {
				$this->Hoso->recursive = -1;
				$hoso = $this->Hoso->findById($id);
				if (!empty($hoso)) {
					
					$this->Hoso->id = $id;
					$this->Hoso->saveField('trang_thai', $this->Status->HS_DA_LUU_TRU);
					$this->Hoso->save();
					
					// update history
					$his = array(); $his['LichsuHoso'] = array();
					$his['LichsuHoso']['user_id'] = $this->Auth->user('id');
					$his['LichsuHoso']['hoso_id'] = $this->Hoso->id;
					$his['LichsuHoso']['noi_dung_giai_quyet'] = 'Hồ sơ chuyển lưu trữ';
					$his['LichsuHoso']['thao_tac'] = 'Hồ sơ chuyển lưu trữ';
					
					$his['LichsuHoso']['status'] = $this->request->data['Hoso']['status'];
					$this->LichsuHoso->create();
					$this->LichsuHoso->save($his);
				}
			}

			$this->redirect("index");
		}
	}
	
	public function trinhduyet ($id = null) {
		if (!empty($this->request->params['pass'][0])) {
			$selections = split(",",$this->request->params['pass'][0]."-1");
			foreach ($selections as $key => $id) {
				$this->Hoso->recursive = -1;
				$hoso = $this->Hoso->findById($id);
				$date2 = new DateTime($hoso['Hoso']['ngay_hen_tra']);
				$date1 = new DateTime();
				if (!empty($hoso) && $hoso['Hoso']['trang_thai'] == $this->Status->HS_DU_YEU_CAU) {
					$this->Hoso->id= $id;
					$this->Hoso->saveField('trang_thai', $this->Status->HS_CHO_KIEM_DUYET);
				//	$this->Hoso->saveField('so_ngay_con_lai', $this->compareDate($date1, $date2));
					$this->Hoso->save();
					
					// update history
					
					$his = array(); $his['LichsuHoso'] = array();
					
					$his['LichsuHoso']['user_id'] = $this->Auth->user('id');
					$his['LichsuHoso']['hoso_id'] = $this->Hoso->id;
					$his['LichsuHoso']['status'] = $this->Status->HS_CHO_KY;
					$his['LichsuHoso']['noi_dung_giai_quyet'] = 'Trình duyệt';
					$his['LichsuHoso']['thao_tac'] = 'Trình duyệt';
					$his['LichsuHoso']['so_ngay_con_lai'] = $this->compareDate($date1, $date2);
					
					$this->LichsuHoso->create();
					$this->LichsuHoso->save($his);
				}
			}
		} else {
			$this->log("Rejecting document: invalid document id= "+$id);
		}
		// $this->render('/index.ctp');
		$this->redirect('index');
	}
	
	public function trinhky ($id = null) {
		if (!empty($this->request->params['pass'][0])) {
			$selections = split(",",$this->request->params['pass'][0]."-1");
			foreach ($selections as $key => $id) {
				$this->Hoso->recursive = -1;
				$hoso = $this->Hoso->findById($id);
				$date2 = new DateTime($hoso['Hoso']['ngay_hen_tra']);
				$date1 = new DateTime();
				if (!empty($hoso) && $hoso['Hoso']['trang_thai'] == $this->Status->HS_DA_DUYET) {
					$this->Hoso->id= $id;
					$this->Hoso->saveField('trang_thai', $this->Status->HS_CHO_KY);
					//$this->Hoso->saveField('so_ngay_con_lai', $this->compareDate($date1, $date2));
					$this->Hoso->save();
					
					// update history
					$his = array(); $his['LichsuHoso'] = array();
					$his['LichsuHoso']['user_id'] = $this->Auth->user('id');
					$his['LichsuHoso']['hoso_id'] = $this->Hoso->id;
					$his['LichsuHoso']['status'] = $this->Status->HS_CHO_KY;
					$his['LichsuHoso']['noi_dung_giai_quyet'] = 'Trình ký';
					$his['LichsuHoso']['thao_tac'] = 'Trình ký';
					$his['LichsuHoso']['so_ngay_con_lai'] = $this->compareDate($date1, $date2);
					
					$this->LichsuHoso->create();
					$this->LichsuHoso->save($his);
				}
			}
		} else {
			$this->log("Rejecting document: invalid document id= "+$id);
		}
		// $this->render('/index.ctp');
		$this->redirect('index');
	}
	
	public function hosohoanthien ($id = null) {
		if (!empty($this->request->data)) {
			$this->Hoso->id = $this->request->data['Hoso']['id'];
			$this->Hoso->saveField('trang_thai', $this->Status->HS_DA_XULY_XONG );
			$this->Hoso->save();
			
			$this->redirect("index");
		}
	}
	public function duyet ($id = null) {
		if (!empty($this->request->data)) {
			$this->Hoso->id = $this->request->data['Hoso']['id'];
			$this->Hoso->saveField('trang_thai', $this->Status->HS_DU_YEU_CAU);
			$this->Hoso->save();
			
			$this->redirect("index");
		}
	}
	
	public function boxunghoso ($id = null) {
		if (!empty($this->request->data)) {
			$this->Hoso->id = $this->request->data['Hoso']['id'];
			$this->Hoso->saveField('trang_thai', $this->Status->BO_SUNG_HS);
			$this->Hoso->save();
			
			$this->redirect("index");
		}
	}
	
	public function tralaihoso ($id = null) {
		if (!empty($this->request->data)) {
			$this->Hoso->id = $this->request->data['Hoso']['id'];
			$this->Hoso->saveField('trang_thai', $this->Status->HS_KHONG_DAT);
			$this->Hoso->save();
			
			$this->redirect("index");
		}
	}
	
	public function tondong ($id = null) {
		if (!empty($this->request->data)) {
			$this->Hoso->id = $this->request->data['Hoso']['id'];
			$this->Hoso->saveField('trang_thai', $this->Status->HS_TON_DONG);
			$this->Hoso->save();
			
			$this->redirect("index");
		}
	}
	
	public function back () {
		$this->redirect('index');
	}
	
	public function ketquahanhchinh ($id = null) {
		$hoso = $this->Hoso->findById($id);
		
		$ketquas = $this->Ketquahanhchinh->find("all", array("conditions" => array("Ketquahanhchinh.hoso_id" => $id)));
		
	
		// calculate ngay qua han
		$date1 = new DateTime();
		$date2 = new DateTime($hoso['Hoso']['ngay_hen_tra']);
		// calculate ngay qua han
		($this->compareDate($date1, $date2) < 0) && $this->set("qua_han", $this->compareDate($date1, $date2));
		
		$this->set("hoso", $hoso);
		$this->set("ketquas", $ketquas);
		$this->set("selectedHoso", $id);
	}
	
	public function vaoso($id = null) {
		if (!empty($this->request->data)) {
			$hoso = $this->Hoso->findById($this->request->data['Ketquahanhchinh']['hoso_id']);
			debug($this->request->data);
			
			$this->log("Insert new ket qua hanh chinh", 'info');
			
			if(isset($this->request->data['Ketquahanhchinh']['ngay_ban_hanh_form'])) {
				$this->request->data['Ketquahanhchinh']['ngay_ban_hanh'] = $this->convertStrDate($this->request->data['Ketquahanhchinh']['ngay_ban_hanh_form']);
			}
			
			if(isset($this->request->data['Ketquahanhchinh']['ngay_co_hieu_luc_form'])) {
				$this->request->data['Ketquahanhchinh']['ngay_co_hieu_luc'] = $this->convertStrDate($this->request->data['Ketquahanhchinh']['ngay_co_hieu_luc_form']);
			}
			
			if(isset($this->request->data['Ketquahanhchinh']['ngay_het_hieu_luc_form'])) {
				$this->request->data['Ketquahanhchinh']['ngay_het_hieu_luc'] = $this->convertStrDate($this->request->data['Ketquahanhchinh']['ngay_het_hieu_luc_form']);
			}
			
			if(isset($this->request->data['Ketquahanhchinh']['ngay_sinh_form'])) {
				$this->request->data['Ketquahanhchinh']['ngay_sinh'] = $this->convertStrDate($this->request->data['Ketquahanhchinh']['ngay_sinh_form']);
			}
			
			if(isset($this->request->data['Ketquahanhchinh']['ngay_cap_form'])) {
				$this->request->data['Ketquahanhchinh']['ngay_cap'] = $this->convertStrDate($this->request->data['Ketquahanhchinh']['ngay_cap_form']);
			}
			
			$this->request->data['Ketquahanhchinh']['tthc_id'] = $hoso['Hoso']['tthc_id'];
			
			//debug($this->request->data);
					
			$this->Ketquahanhchinh->create();
			$this->Ketquahanhchinh->save($this->request->data);
			
			$this->redirect(array('action' => 'ketquahanhchinh', $id));
		}
		
		// $tthcs = $this->Tthc->find("list", array('fields'=>array('id','ten'),'conditions' => array('activeFlg' => 1)));
		$loaihinhs = $this->LoaiHinh->find("list", array('fields'=>array('id','ten'),'conditions' => array('activeFlg' => 1)));
		$linhvucs = $this->LinhVuc->find("list", array('fields'=>array('id','ten'),'conditions' => array('activeFlg' => 1)));
		$thanhPhos = $this->ThanhPho->find("list", array('fields'=>array('code','ten')));
		
		
		$this->set("thanhPhos", $thanhPhos);
		$this->set("loaihinhs", $loaihinhs);
		$this->set("linhvucs", $linhvucs);
		$this->set("selectedHoso", $id);
		// debug($linhvucs);
	}

	public function deleteKqhc($id = null,$selectedHoso = null) {
		if (!empty($id)) {
			$this->Ketquahanhchinh->delete($id);
			$this->redirect(array('action' => 'ketquahanhchinh', $selectedHoso));
		}
	}
	
	public function updateKqhc($id = null,$selectedHoso = null) {
		if (!empty($id)) {
			$ketqua = $this->Ketquahanhchinh->findById($id);
			
			$this->request->data['Ketquahanhchinh'] = $ketqua['Ketquahanhchinh'];
			
			$this->request->data['Ketquahanhchinh']['ngay_ban_hanh_form'] = $ketqua['Ketquahanhchinh']['ngay_ban_hanh'];
			$this->request->data['Ketquahanhchinh']['ngay_co_hieu_luc_form'] = $ketqua['Ketquahanhchinh']['ngay_co_hieu_luc'];
			$this->request->data['Ketquahanhchinh']['ngay_het_hieu_luc_form'] = $ketqua['Ketquahanhchinh']['ngay_het_hieu_luc'];
			$this->request->data['Ketquahanhchinh']['ngay_sinh_form'] = $ketqua['Ketquahanhchinh']['ngay_sinh'];
			$this->request->data['Ketquahanhchinh']['ngay_cap_form'] = $ketqua['Ketquahanhchinh']['ngay_cap'];
			$this->request->data['Ketquahanhchinh']['ngay_cap_ma_form'] = $ketqua['Ketquahanhchinh']['ngay_cap_ma'];
			
			$tthcs = $this->Tthc->find("list", array('fields'=>array('id','ten'),'conditions' => array('activeFlg' => 1)));
			$loaihinhs = $this->LoaiHinh->find("list", array('fields'=>array('id','ten'),'conditions' => array('activeFlg' => 1)));
			$linhvucs = $this->LinhVuc->find("list", array('fields'=>array('id','ten'),'conditions' => array('activeFlg' => 1)));
			$thanhPhos = $this->ThanhPho->find("list", array('fields'=>array('code','ten')));
			
			
			$this->set("thanhPhos", $thanhPhos);
			$this->set("loaihinhs", $loaihinhs);
			$this->set("linhvucs", $linhvucs);
			$this->set("tthcs", $tthcs);
			$this->set("selectedHoso", $selectedHoso);
			
		} else {
			$this->log("Invalid kqhc id",'info');
		}
	}
	
	public function lapPhieuTrinh($id = null) {
		
		if (!empty($this->request->data)) {
			$this->log("Update new phieu trinh", 'info');
			
			$uploadFile = $this->request->data['PhieuTrinh']['phieu_trinh'];
			$newFileName = $uploadFile['name'];
			$this->log("Move file to: ".WWW_ROOT.'files/phieutrinh/'.$newFileName);
			move_uploaded_file($uploadFile['tmp_name'], WWW_ROOT.'files/phieutrinh/'.$newFileName);
			
			if(isset($this->request->data['PhieuTrinh']['ngay_form'])) {
				$this->request->data['PhieuTrinh']['ngay'] = $this->convertStrDate($this->request->data['PhieuTrinh']['ngay_form']);
			}

			$phieu = $this->PhieuTrinh->findByHosoId($id);
			if (empty($phieu)) {
				$phieu = array();
				$phieu['PhieuTrinh'] = array();
			}
			
			$phieu['PhieuTrinh']['hoso_id'] = $this->request->data['PhieuTrinh']['hoso_id'];
			$phieu['PhieuTrinh']['so'] = $this->request->data['PhieuTrinh']['so'];
			$phieu['PhieuTrinh']['ngay'] = $this->request->data['PhieuTrinh']['ngay'];
			$phieu['PhieuTrinh']['kinh_gui'] = $this->request->data['PhieuTrinh']['kinh_gui'];
			$phieu['PhieuTrinh']['can_bo_trinh'] = $this->request->data['PhieuTrinh']['can_bo_trinh'];
			$phieu['PhieuTrinh']['ve_viec'] = $this->request->data['PhieuTrinh']['ve_viec'];
			$phieu['PhieuTrinh']['y_kien_ca_bo_xuly'] = $this->request->data['PhieuTrinh']['y_kien_ca_bo_xuly'];
			$phieu['PhieuTrinh']['y_kien_kiem_duyet'] = $this->request->data['PhieuTrinh']['y_kien_kiem_duyet'];
			$phieu['PhieuTrinh']['y_kien_lanh_dao'] = $this->request->data['PhieuTrinh']['y_kien_lanh_dao'];
			$phieu['PhieuTrinh']['phieu_trinh_file'] = $uploadFile['name'];
			
			if(isset($this->request->data['PhieuTrinh']['ngay_form'])) {
				$this->request->data['PhieuTrinh']['ngay'] = $this->convertStrDate($this->request->data['PhieuTrinh']['ngay_form']);
			} 
			
			//debug($phieu);
			
			$this->PhieuTrinh->save($phieu);
			
			//$this->redirect(array('action' => 'xuly', $id));
		}

		if (!empty($id)) {
			$phieu = $this->PhieuTrinh->findByHosoId($id);
			$hoso = $this->Hoso->findById($id);
			//debug($phieu);
			
			$this->request->data['PhieuTrinh'] = $phieu["PhieuTrinh"];
			if(!isset($phieu["PhieuTrinh"]['ngay'])) {
				$this->set('ngay',date('d/m/Y'));
			}
			
			// $this->set("hoso", $hoso);
			// debug($this->isEdit($hoso));
			$this->set("editable", $this->isEdit($hoso));
		}
		
		$this->request->data['PhieuTrinh']['can_bo_trinh'] = $this->Auth->user('ho_ten');
		$this->set("selectedHoso", $id);
	}
	
	/**
	 * Xem Hoso truoc de xuly
	 */
	public function xemHoso ($id = null) {
		if (!empty($this->request->data)) {
			$date = !empty($this->request->data['Hoso']['ngay_tra'])?$this->convertStrDate($this->request->data['Hoso']['ngay_tra']): NULL;
			
			$this->Hoso->id = $this->request->data['Hoso']['id'];
			$this->Hoso->saveField('ghi_chu', $this->request->data['Hoso']['ghi_chu']);
			!empty($date)?$this->Hoso->saveField('ngay_hen_tra', $date):'';
			$this->Hoso->save();
			
			$this->redirect(array("action" => "xuly",$this->Hoso->id));
		}
		
		$hoso = $this->Hoso->findById($id);
		$thanhPhos = $this->ThanhPho->find("list", array('fields'=>array('code','ten')));
		$this->set('thanhPhos', $thanhPhos);
		
		if (!empty($hoso)){
			$thanhPhans = $this->ThanhPhan->getListThanhPhanGiayToByTthcId($hoso['Tthc']['id']);
			$this->set("thanhPhans", $thanhPhans);
			
			$attachments = $this->Attachment->find("all", array('conditions' => array("hoso_id" => $id)));
			$this->set("attachments", $attachments);
		} else {
			$this->log("Invalid document Id:"+$id);
		}
		
		$hoso['Hoso']['ngay_tra'] = empty($hoso['Hoso']['ngay_hen_tra'])?"":CakeTime::format('d/m/Y',$hoso['Hoso']['ngay_hen_tra']);
		//export value to view
		$this->set("hoso", $hoso);
		$this->set("selectedHoso", $id);
	}

	/**
	 * Bao cao thong ke $id = ketquahanhchinh_id,$selectedHoso = hoso_id
	 */
	public function baocaoTk ($id = null, $selectedHoso = null) {
		$kqhc = $this->Ketquahanhchinh->findById($id);
		$chiTieus = $this->ChiTieuThongKe->getSolieuThongKeByKqhc($id,$kqhc['Ketquahanhchinh']['tthc_id']);
		// debug($chiTieus);
		
		if (!empty($this->request->data)) {
			// debug($this->request->data);

			foreach ($chiTieus as $key => $chiTieu) {
				if (empty($this->request->data['ThongKe'][$chiTieu['ct']['id']])) {
					$this->log('Khong cap nhat so lieu thong ke cho chitieu nay -> ignore: '.$chiTieu['ct']['id']);
					continue;
				}
				
				$thongke = array();$thongke['ThongKe'] = array();
				
				$solieuTk = $this->ThongKe->find("all", array("conditions" => 
					array('ketquahanhchinh_id' => $this->request->data['ThongKe']['ketquahanhchinh_id'], 'chi_tieu_thong_ke_id' => $chiTieu['ct']['id'])));
					
				if (empty($solieuTk)) {
					$this->log("Create new solieu thong ke");
					
					$thongke['ThongKe']['ketquahanhchinh_id'] = $this->request->data['ThongKe']['ketquahanhchinh_id'];
					$thongke['ThongKe']['chi_tieu_thong_ke_id'] = $chiTieu['ct']['id'];
					$thongke['ThongKe']['so_lieu'] = $this->request->data['ThongKe'][$chiTieu['ct']['id']];
					
					$this->ThongKe->create();
					$this->ThongKe->save($thongke);	
				} else {
					$this->log("Update solieu thong ke");
					$solieu = $solieuTk[0];
					
					$this->ThongKe->read(null, $solieu['ThongKe']['id']);
					$this->ThongKe->set('so_lieu', $this->request->data['ThongKe'][$chiTieu['ct']['id']]);
					$this->ThongKe->save();
				}
				
				$this->Session->setFlash("Luu du lieu thanh cong");
			}
			$this->redirect(array('action' =>"baocaoTk",$id,$selectedHoso));
		}
		
		$this->set("selectedHoso", $selectedHoso);
		$this->set("ketquahanhchinh", $id);
		$this->set("chiTieus", $chiTieus);
	}

	/**
	 * Bao cao phan loai
	 */
	public function baocaoPl ($id = null, $selectedHoso = null) {
		$chiTieus = $this->ChiTieuPhanLoai->getSolieuPhanloaiByKqhc($id);
		// debug($chiTieus);
		
		if (!empty($this->request->data)) {
			// debug($this->request->data);

			foreach ($chiTieus as $key => $chiTieu) {
				if (empty($this->request->data['PhanLoaiHoso'][$chiTieu['pl']['id']])) {
					$this->log('Khong cap nhat so lieu phan loai cho chitieu nay -> ignore: '.$chiTieu['pl']['id']);
					continue;
				}
				
				$phanloai = array();$phanloai['PhanLoaiHoso'] = array();
				
				$solieuTk = $this->PhanLoaiHoso->find("all", array("conditions" => 
					array('ketquahanhchinh_id' => $this->request->data['PhanLoaiHoso']['ketquahanhchinh_id'], 'chi_tieu_phan_loai_id' => $chiTieu['pl']['id'])));
					
				if (empty($solieuTk)) {
					$this->log("Create new solieu thong ke");
					
					$phanloai['PhanLoaiHoso']['ketquahanhchinh_id'] = $this->request->data['PhanLoaiHoso']['ketquahanhchinh_id'];
					$phanloai['PhanLoaiHoso']['chi_tieu_phan_loai_id'] = $chiTieu['pl']['id'];
					$phanloai['PhanLoaiHoso']['so_lieu'] = $this->request->data['PhanLoaiHoso'][$chiTieu['pl']['id']];
					
					$this->PhanLoaiHoso->create();
					$this->PhanLoaiHoso->save($phanloai);	
				} else {
					$this->log("Update solieu phan loai");
					$solieu = $solieuTk[0];
					
					$this->PhanLoaiHoso->read(null, $solieu['PhanLoaiHoso']['id']);
					$this->PhanLoaiHoso->set('so_lieu', $this->request->data['PhanLoaiHoso'][$chiTieu['pl']['id']]);
					$this->PhanLoaiHoso->save();
				}
				
				$this->Session->setFlash("Luu du lieu thanh cong");
			}
			$this->redirect(array('action' =>"baocaoPl",$id,$selectedHoso));
		}
		
		$this->set("selectedHoso", $selectedHoso);
		$this->set("ketquahanhchinh", $id);
		$this->set("chiTieus", $chiTieus);
	}

	public function chuyenTraPxl ($ids = null) {
		if (!empty($this->request->params['pass'][0])) {
			$selections = split(",",$this->request->params['pass'][0]."-1");
			foreach ($selections as $key => $id) {
				$this->Hoso->recursive = -1;
				$hoso = $this->Hoso->findById($id);
				$date1 = new DateTime();
				$date2 = new DateTime($hoso['Hoso']['ngay_hen_tra']);
					
				if (!empty($hoso)) {
					$this->Hoso->id= $id;
					$this->Hoso->saveField('trang_thai', $this->Status->CHO_PHAN_XULY);
					//$this->Hoso->saveField('so_ngay_con_lai', $this->compareDate($date1, $date2));
					
					$this->Hoso->save();
					
					// update history
					
					$his = array(); $his['LichsuHoso'] = array();
					
					$his['LichsuHoso']['user_id'] = $this->Auth->user('id');
					$his['LichsuHoso']['hoso_id'] = $this->Hoso->id;
					$his['LichsuHoso']['status'] = $this->Status->CHO_PHAN_XULY;
					$his['LichsuHoso']['noi_dung_giai_quyet'] = 'Trả lại phân xử lý';
					$his['LichsuHoso']['thao_tac'] = 'Trả lại phân xử lý';
					$his['LichsuHoso']['so_ngay_con_lai'] = $this->compareDate($date1, $date2);
					
					$this->LichsuHoso->create();
					$this->LichsuHoso->save($his);
				}
			}
		} else {
			$this->log("Rejecting document: invalid document id= "+$id);
		}
		// $this->render('/index.ctp');
		$this->redirect('index');
	}
}
